K.define('kuser.kzjbjlist',{
   kpartname:'工作任务信息',//模块名称
   gridid:"kzjbj_gridlist",//表格列表id
   addwin:"kzjbj_addwin",//添加弹出win id
   updwim:"kzjbj_updatewin",//修改弹出win id
   editFlag:undefined,
   /**
    * 初始化
    * @param me
    * @param type
    */
   init: function (me,type) {
		me.kuser_gridlist(me);
	},
	/**
	 * 渲染表格
	 * @param me
	 */
   kuser_gridlist:function(me){
	   var listid=me.gridid;
	   $("#"+listid,me.contentTarget).datagrid({
		      url:"./v1/searchkettlelist",
		      queryParams:{},
		      title:'',
		      width:'98%',
		      rownumbers: true, //是否显示列数 
		      loadMsg: "正在加载数据...",
		      pagination:true ,
		      //pagePosition:"top",
		      fit: true,   //自适应大小
			  border:false,
			  nowrap: true,//数据长度超出列宽时将会自动截取。
			  fitColumns:true,//自动使列适应表格宽度以防止出现水平滚动。
			  singleSelect:false,
			  autoRowHeight:true,  
			  toolbar: [{
			  	 text:'添加',
				 iconCls: 'icon-add',
				 handler: function(){
					 me.kuser_add(me);
					 }
				},'-',{
			  	 text:'修改',
				 iconCls: 'icon-edit',
				 handler: function(){
					 me.kuser_kupiswin(me);
					}
				},'-',{
			  	 text:'删除',
				 iconCls: 'icon-remove',
				 handler: function(){
					 me.kuser_deletedata(me);
					}
				},'-',{
				  	 text:'立即执行',
					 iconCls: 'icon-redo',
					 handler: function(){
						 me.kuser_startjob(me);
					}
				},'-',{
				  	 text:'立即执行quartz',
					 iconCls: 'icon-redo',
					 handler: function(){
						 me.kuser_quartzjob(me);
					}
				}],
		      columns:[[
		         /* { field:'ck',checkbox:true },*/
		         {field:'title',title:'工作标题',width:60,align:'center'},
		          {field:'jobname',title:'工作名称',width:60,align:'center'},
		          {field:'jobgroupname',title:'工作组名',width:60,align:'center'},
		          {field:'createtime',title:'创建时间',width:80,align:'center',formatter : function(value, row, index) {
	                    if (value == null || value == '') {
	                        return '';
	                    }
	                    var dt;
	                    if (value instanceof Date) {
	                        dt = value;
	                    } else {
	                        dt = new Date(value);
	                    }
	                    return dt.format("yyyy-MM-dd hh:mm:ss");
	                }
		          },
		          {field:'czzt',title:'执行状态',width:120,align:'center',formatter: function(value,row,index){
		        	  var kval="";
		        	  if(value){
		        		  if(value=="1"){
			        		  kval="执行成功";
			        	  }else if(value=="3"){
			        		  kval="正在执行中";
			        	  }else{
			        		  kval="执行失败";
			        	  }
		        	  }
		        	  return kval;
		          }},
		          {field:'kssj',title:'开始时间',width:90,align:'center',
		        	  formatter :  function(value, row, index) {
		                    if (value == null || value == '') {
		                        return '';
		                    }
		                    var dt;
		                    if (value instanceof Date) {
		                        dt = value;
		                    } else {
		                        dt = new Date(value);
		                    }
		                    return dt.format("yyyy-MM-dd hh:mm:ss");
		                }},
		          {field:'jssj',title:'结束时间',width:90,align:'center',
		        	  formatter :  function(value, row, index) {
		                    if (value == null || value == '') {
		                        return '';
		                    }
		                    var dt;
		                    if (value instanceof Date) {
		                        dt = value;
		                    } else {
		                        dt = new Date(value);
		                    }
		                    return dt.format("yyyy-MM-dd hh:mm:ss");
		                }},{
			                field : 'bz',
			                title : '描述信息',
			                width : 80
			            }
		      ]],
		       onLoadSuccess:function(data){  
		        $("#"+listid,me.contentTarget).datagrid('fixRowHeight');  
		    },
		    onDblClickRow :function(rowIndex,rowData){
		    	if(rowData){
		              me.kuser_update(me,rowData);
		         }
		    	}
		  }); 
		  var p = $("#"+listid,me.contentTarget).datagrid("getPager");  
		  $(p).pagination({  
		       pageSize: 10,//每页显示的记录条数，默认为10 
		       pageList: [10, 20, 30],//可以设置每页记录条数的列表  
		       beforePageText: '第',//页数文本框前显示的汉字  
		       afterPageText: '页    共 {pages} 页',  
		       displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
		   }); 
	  },
	  /**
	   * 查询
	   * @param me
	   * @param formid
	   */
	 selectkuser:function(me,formid){
	     var listid=me.gridid;
	     var ktitle=$("#ktitles",'#'+formid,me.contentTarget).val();//名称
		 var param = {"title" :ktitle};
		 $('#'+listid,me.contentTarget).datagrid("load",param);
	},
	repalceval:function(kval){
		if(kval=="null" || !kval){
			return "";
		}
		return kval;
	},
  kuser_kupiswin:function(me){
		var rows = $("#kzjbj_gridlist",me.contentTarget).datagrid('getSelections');
		if(rows.length!=1){
			 K.kmessager('提示','选中一条要调整的表单');  
			 return false;
		}
		var kid=rows[0];
	    me.kuser_update(me,kid);
	
	},
	/**
	 * 弹出添加界面
	 * @param me
	 */
	kuser_add:function(me){
		 K.tpl({
				tpl: "static/resources/template/kuser/kzjbjlist_win.html",
				success: function(html,data){
					var dialogs = $('<div></div>').dialog({
						title: '添加工作任务',
						width:'600',
					    height:'300',
					    iconCls: 'icon-add',
					    content:html,
					    modal: true,
					    //fit: true, 
					    resizable: true,  //控制窗体拉伸
		               /* minimizable: true,//控制最小化
		                maximizable: true, //控制最大化  
*/					    buttons:[{text:'保存',iconCls:"icon-save",handler:function(){
					    	 me.upresult_save(me,dialogs,1);
					    }},{text:'取消',iconCls:"icon-no",handler:function(){
					            	  $(dialogs).dialog('destroy');
					            	  }}
					                ],
					    closed: false,
					    cache: false,
					    onClose : function() {
		                     $(this).dialog('destroy');
		                 },
		                 onOpen:function(){
		                	 me.onwinsize();
		                 },
		                 onResize:function(){
		                	 me.onwinsize();
		                 },
		                 onMove:function(){
		                	 me.onwinsize();
		                 }
					});
				}
			}); 
  },
  /**
   * 修改弹出界面
   */
	kuser_update:function(me,rowData){
	 K.tpl({
			tpl: "static/resources/template/kuser/kzjbjlist_win.html",
			//url:"kmaincountents/showmsgById",
			//data:{"kid":kid},
			before: function(data){
				if(!rowData.filepath){
					rowData.rowData="/";
				}
				return rowData;
			},
			success: function(html,data){
				var dialogs = $('<div></div>').dialog({
					title: '修改工作任务',
					width:'600',
				    height:'300',
				    iconCls: 'icon-edit',
				    content:html,
				    modal: true,
				    //fit: true, 
				    resizable: true,  //控制窗体拉伸
	               /* minimizable: true,//控制最小化
	                maximizable: true, //控制最大化*/
				    buttons:[
				    {text:'保存',iconCls:"icon-save",handler:function(){
				    	me.upresult_save(me,dialogs,2);
				    }},{text:'关闭',iconCls:"icon-no",handler:function(){
	            	    $(dialogs).dialog('destroy');
	            	  }}
	                ],
				    closed: false,
				    cache: false,
				    onOpen:function(){
	                	 me.onwinsize();
	                 },
	                 onResize:function(){
	                	 me.onwinsize();
	                 },
	                 onMove:function(){
	                	 me.onwinsize();
	                 },
				    onClose : function() {
	                     $(this).dialog('destroy');
	                 }
				});			       
			}
		}); 
	},
	  kuser_deletedata:function(me){
		    var rows = $("#kzjbj_gridlist",me.contentTarget).datagrid('getSelections');
			if(rows.length<=0){
				 K.kmessager('提示','至少要选择一条要删除的表单');  
				 return false;
			}
		   $.messager.confirm('提醒','确定要删除该记录吗?',function(isok){
			    if (isok){
			    	 $.ajax({
						  type:"post",
						  url:"./v1/deletekettle",
						  dataType:"json",
						  data:rows[0],
						  success: function(result) {
							  if(result){
								 var sucess=result.sucess;
								 $("#"+me.gridid,me.contentTarget).datagrid('reload');
								 K.kmessager('提示',result.msg);
							  }else{
								  K.kmessager('提示',result.msg);
							  }
						  }
			    });
			   }
			 });
	  },
	onwinsize:function(){
		 $(".window").css("z-index","1001");
         $(".window-shadow").css("z-index","900");
         $(".window-mask").css("z-index","900");
	},
	upresult_save:function(me,dialogs,ktype){
		  $.messager.progress();
		  var kurl= "./v1/savekettle";
		  if(ktype=="2"){
			  kurl= "./v1/updatekettle";
		  }
		  $('#kzjbjlist_myform',dialogs).form('submit', {
		  	url: kurl,
		  	type:"post",
			dataType:"json",
		  	onSubmit: function(){
		  		var isValid = $(dialogs).form('validate');
		  		if (!isValid){
		  			$.messager.progress('close');	
		  		}
		  		return isValid;	
		  	},
		  	error:function (data, status, e){   
		  		 K.kmessager('提示','出现异常！');
			},  
		  	success: function(result){
		  		$.messager.progress('close');
		  		if(result){
		  			 dialogs.dialog('destroy');
					 $("#"+me.gridid,me.contentTarget).datagrid('reload');
					 var datajson=$.parseJSON(result);
					 K.kmessager('提示',datajson.msg);
				  }else{
					  K.kmessager('提示','数据提交失败！');
				  }
		  	  }
		  });
	 },
	 kuser_startjob:function(me){
		 var kurl="";
		 var rows = $("#kzjbj_gridlist",me.contentTarget).datagrid('getSelections');
		 if(rows.length!=1){
			 K.kmessager('提示','选中一条要调整的表单');  
			 return false;
		 }
		var personString = JSON.stringify(rows[0]);
		$.messager.confirm('提醒','确定要立即执行吗?',function(isok){
		if (isok){
		 $.ajax({
			  type:"post",
			  url:'./v1/executejob',
			  dataType: 'json',
			  contentType:"application/json; charset=utf-8",
			  data:personString,
			  success: function(result) {
				  if(result){
					 var sucess=result.success;
					 K.kmessager('提示',result.zxbzlog);
					 $("#"+me.gridid,me.contentTarget).datagrid('reload');
				  }else{
					  K.kmessager('提示','操作失败！');
				  }
			  }
	     });
		}
		});
	 },
	 kuser_quartzjob:function(me){
		 var kurl="";
		 var rows = $("#kzjbj_gridlist",me.contentTarget).datagrid('getSelections');
		 if(rows.length!=1){
			 K.kmessager('提示','选中一条要调整的表单');  
			 return false;
		 }
		var personString = JSON.stringify(rows[0]);
		$.messager.confirm('提醒','确定要立即执行quartz吗?',function(isok){
		if (isok){
		 $.ajax({
			  type:"post",
			  url:'./v1/executequartzjob',
			  dataType: 'json',
			  contentType:"application/json; charset=utf-8",
			  data:personString,
			  success: function(result) {
				  if(result){
					 var sucess=result.success;
					 K.kmessager('提示',result.zxbzlog);
					 $("#"+me.gridid,me.contentTarget).datagrid('reload');
				  }else{
					  K.kmessager('提示','操作失败！');
				  }
			  }
	     });
		}
		});
	 }
}, true);